package com.heima.article.controller;

import com.heima.article.service.ApArticleService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/article")
public class ApArticleController {

    @Autowired
    private ApArticleService apArticleService;


    /**
     * 根据频道获取文章分页列表
     * @param channelId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list/{tag}/{page}/{size}")
    public ResponseResult list(
            @PathVariable(value = "tag") Integer channelId,
            @PathVariable(value = "page") Integer page,
            @PathVariable(value = "size") Integer size
    ){
        page = page <= 0 ? 1 : page;
        size = size <= 0 || size >100 ? 100 : size;
        return apArticleService.list(channelId , page , size);
    }

    /**
     * 根据id修改文章
     * @param apArticle
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody ApArticle apArticle){
        apArticleService.updateById(apArticle);
        return ResponseResult.okResult(null);
    }

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable(value = "id" ,required = true) Long id){
        apArticleService.removeById(id);
        return ResponseResult.okResult(null);
    }


}
